package com.aek.aekdroid;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.aek.aekdroid.data.IDbConstants;
import com.aek.aekdroid.data.MatchData;

public class MatchProvider extends ContentProvider {
	public static final String TAG = "MatchProvider";

	public static final Uri CONTENT_URI = Uri.parse("content://com.aek.aekdroid.matchprovider");
	public static final String SINGLE_REC = "vnd.android.cursor.item/vnd.aek.aekdroid.match";
	public static final String MULTI_REC = "vnd.android.cursor.dir/vnd.aek.aekdroid.match";
	private MatchData matchData;
	public static final String SORT_ORDER = MatchData.MATCHES_C_DATETIME + " ASC";

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		long id = getId(uri);
		int count;
		SQLiteDatabase db = matchData.dbHelper.getWritableDatabase();
		try {
			if (id < 0) {
				count = db.delete(IDbConstants.MATCHES_TABLE, selection, selectionArgs);
			} else {
				count = db.delete(IDbConstants.MATCHES_TABLE, IDbConstants.MATCHES_C_ID + "=?", new String[] { String.valueOf(id) });
			}
		} finally {
			db.close();
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public String getType(Uri uri) {
		return this.getId(uri) > 0 ? SINGLE_REC : MULTI_REC;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		SQLiteDatabase db = matchData.dbHelper.getWritableDatabase();
		try {
			long id = db.insertOrThrow(IDbConstants.MATCHES_TABLE, null, values);
			if (id == -1) {
				throw new RuntimeException(TAG + ": Failed to insert data");
			} else {
				getContext().getContentResolver().notifyChange(uri, null);
				return ContentUris.withAppendedId(uri, id);
			}
		} finally {
			db.close();
		}
	}

	@Override
	public boolean onCreate() {
		matchData = new MatchData(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		MyLog.d(TAG, "Query");
		long id = getId(uri);
		Cursor c;
		SQLiteDatabase db = matchData.dbHelper.getReadableDatabase();

		if (id < 0) {
			c = db.query(IDbConstants.MATCHES_TABLE, projection, selection, selectionArgs, null, null, sortOrder);
		} else {
			c = db.query(IDbConstants.MATCHES_TABLE, projection, IDbConstants.MATCHES_C_ID + "=?", new String[] { String.valueOf(id) },
					null, null, null);
		}
		c.setNotificationUri(getContext().getContentResolver(), uri);
		return c;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		long id = this.getId(uri);
		int count;
		SQLiteDatabase db = matchData.dbHelper.getWritableDatabase();
		try {
			if (id < 0) {
				count = db.update(IDbConstants.MATCHES_TABLE, values, selection, selectionArgs);
			} else {
				count = db
						.update(IDbConstants.MATCHES_TABLE, values, IDbConstants.MATCHES_C_ID + "=?", new String[] { String.valueOf(id) });
			}
		} finally {
			db.close();
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return count;

	}

	private long getId(Uri uri) {
		String lastPathSegment = uri.getLastPathSegment();
		if (lastPathSegment != null) {
			try {
				return Long.parseLong(lastPathSegment);
			} catch (NumberFormatException e) {
				return -1;
			}
		}
		return -1;
	}

	/**
	 * @return the matchData
	 */
	public MatchData getMatchData() {
		return matchData;
	}

}
